package scfg.utils;

import scfg.AlignedSequence;

public class NativeStructureAligner {

	public static String[] alignNativeSequences(AlignedSequence a, AlignedSequence b) {
		if (a==null || b == null)
			return null;
		StringBuilder asb = new StringBuilder();
		StringBuilder bsb = new StringBuilder();
		StringBuilder asbStr = new StringBuilder();
		StringBuilder bsbStr = new StringBuilder();
		StringBuilder asbMfe = new StringBuilder();
		StringBuilder bsbMfe = new StringBuilder();
		String aRef = a.getStructure("refrence").getStructure();
		String bRef = b.getStructure("refrence").getStructure();
		String aMfe = a.getStructure("mfe_gt").getStructure();
		String bMfe = b.getStructure("mfe_gt").getStructure();
		int aInd = 0;
		int bInd = 0;
		for(int i=0;i<a.getAlignment().length();i++) {
			char ac = a.getAlignment().charAt(i);
			char bc = a.getAlignment().charAt(i);
			if (ac == bc && ac == '-') {
				// DO NOTHING
			} else {
				if (ac == '-' || aInd >= aRef.length()) {
					asb.append('n');
					asbStr.append('.');
					asbMfe.append('.');
				} else {
					asb.append(ac);
					asbStr.append(aRef.charAt(aInd));
					asbMfe.append(aMfe.charAt(aInd++));
				}
				if (bc == '-' || bInd >= bRef.length()) {
					bsb.append('n');
					bsbStr.append('.');
					bsbMfe.append('.');
				} else {
					bsb.append(bc);
					bsbStr.append(bRef.charAt(bInd));
					bsbMfe.append(bMfe.charAt(bInd++));
				}
			}
		}
		String[] arr = { asb.toString(), asbStr.toString(), asbMfe.toString(), bsb.toString(), bsbStr.toString(), bsbMfe.toString() };
		return arr;
	}
	
}
